package com.zmj.sy.mom.srv.aps.mapper;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zmj.sy.mom.srv.aps.bean.entity.order.ApsDetailWeekday;
import com.zmj.sy.mom.srv.aps.bean.entity.order.Order;
import com.zmj.sy.mom.srv.aps.bean.vo.order.OrderGroupLoadsDataResVo;
import com.zmj.sy.mom.srv.aps.bean.vo.order.OrderGroupLoadsListResVo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface OrderMapper extends SyMapper<Order> {
    @Select(" select adw.shift_day, adw.group_id, sum(awd.total_count*awd.work_time) usedWorkTime " +
            " from tb_aps_detail_weekday adw " +
            " left join tb_aps_work_detail awd on adw.aps_work_detail_id = awd.id  " +
            " ${ew.customSqlSegment} ")
    List<OrderGroupLoadsDataResVo> groupLoadsold(@Param(Constants.WRAPPER) QueryWrapper<ApsDetailWeekday> query);
    /**
     * 获取订单组负载数据
     SELECT
     DATE(CASE  WHEN HOUR(awd.plan_start_date_time) < 6 THEN awd.plan_start_date_time - INTERVAL 1 DAY ELSE awd.plan_start_date_time END) AS work_date,
     group_code, SUM(awd.work_time * awd.total_count) AS total_work
     FROM tb_aps_work_detail awd
     WHERE awd.plan_start_date_time >= '2025-09-02 06:00:00'
     AND awd.plan_start_date_time < '2025-09-11 06:00:00'
     GROUP BY work_date, group_code
     */

    @Select(" SELECT" +
            "     DATE(CASE  WHEN HOUR(awd.plan_start_date_time) < 6 THEN awd.plan_start_date_time - INTERVAL 1 DAY ELSE awd.plan_start_date_time END) AS shift_day," +
            "     awd.group_id, SUM(awd.work_time * awd.total_count) AS usedWorkTime " +
            "     FROM tb_aps_work_detail  awd " +
            " ${ew.customSqlSegment} ")
    List<OrderGroupLoadsDataResVo> groupLoads(@Param(Constants.WRAPPER) QueryWrapper<ApsDetailWeekday> query);

    /**
     SELECT
     DATE(CASE  WHEN HOUR(mwd.actual_end_date_time) < 6 THEN mwd.actual_end_date_time - INTERVAL 1 DAY ELSE mwd.actual_end_date_time END) AS work_date,
     awd.group_code, SUM(awd.work_time * mwd.finish_count) AS total_work
     FROM tb_aps_work_detail awd INNER JOIN tb_mes_work_detail_task mwd on awd.id=mwd.aps_work_detail_id
     WHERE mwd.actual_end_date_time >= '2025-08-20 06:00:00'
     AND mwd.actual_end_date_time < '2025-08-29 06:00:00'
     GROUP BY work_date, awd.group_code
     */
    @Select(" SELECT " +
            "     DATE(CASE  WHEN HOUR(mwd.actual_end_date_time) < 6 THEN mwd.actual_end_date_time - INTERVAL 1 DAY ELSE mwd.actual_end_date_time END) AS shift_day," +
            "     awd.group_id, SUM(awd.work_time * mwd.finish_count) AS finishWorkTime" +
            "     FROM tb_aps_work_detail awd INNER JOIN tb_mes_work_detail_task mwd on awd.id=mwd.aps_work_detail_id " +
            " ${ew.customSqlSegment} ")
    List<OrderGroupLoadsDataResVo> groupFishLoads(@Param(Constants.WRAPPER) QueryWrapper<ApsDetailWeekday> query);

    @Select("select group_id, sum(adw.work_time) usedWorkTime " +
            "from tb_aps_detail_weekday adw " +
            "${ew.customSqlSegment} ")
    List<OrderGroupLoadsListResVo> groupLoadsList(@Param(Constants.WRAPPER) QueryWrapper<ApsDetailWeekday> query);

    @Select("delete from tb_order where id = #{orderId}")
    void deleteReal(@Param("orderId") Integer orderId);

    @Select(" select  ob.mater_code " +
            " from tb_order_bom ob  " +
            " left join tb_order o on ob.order_id = o.id " +
            " where ob.`level` = 2 and (o.serial_code = o.mater_code or o.suffix in ('C', 'L', 'W')) and o.construct_no like #{constructNo} ")
    List<String> selectCeban(@Param("constructNo") String orderCode);

    @Select(" select  ob.mater_code " +
            " from tb_order_bom ob  " +
            " left join tb_order o on ob.order_id = o.id " +
            " where ob.`level` = 1 and o.serial_code <> o.mater_code and ob.work_chain <> 'WW' and o.construct_no = #{constructNo} ")
    List<String> selectThreePiece(@Param("constructNo")String orderNo);
    @Select("select t.* " +
            "from tb_order t " +
            "${ew.customSqlSegment} ")
    Page<Order> selectMbdPage(Page<Order> page, @Param(Constants.WRAPPER) QueryWrapper<Order> query);
    @Select("select t.* " +
            "from tb_order t " +
            "where t.id=#{id} LIMIT 1 ;")
    Order selectOneAll(@Param("id") Integer id);
    @Select("select t.* from tb_order t ${ew.customSqlSegment}")
    List<Order> selectMbdList(@Param(Constants.WRAPPER) QueryWrapper<?> query);

    @Select(" select * from tb_order where mater_code = #{materCode} and order_code = #{orderCode} and version is not null order by version desc limit 1 ")
    Order getLastVersion(@Param("materCode") String materCode, @Param("orderCode") String orderCode);

    @Select("select * from tb_order o where o.order_type in (2, 5) ")
    List<Order> findMbd();
}
